package com.example.springboot.service.Impl;

import com.example.springboot.entity.UserEntity;
import com.example.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity userEntity = userMapper.findByUsername(username);
        if (userEntity == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
        List<String> roles = new ArrayList<>();
        roles.add("ROLE_USER"); // 根据实际需求，可以从数据库中获取用户的角色信息
        return User.withUsername(userEntity.getUsername())
                .password(userEntity.getPassword())
                .roles(roles.toArray(new String[0]))
                .build();
    }
}